﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
namespace BusinessLogic
{
    public class PhieuXuatHuy 
    {
        Entities.ChiTietXuatHuy[] chitietxuathuy;
        public static Entities.PhieuXuatHuy[] Select(Entities.PhieuXuatHuy nv)
        {
            Entities.PhieuXuatHuy[] list = null;
            try
            {
                string sql = Common.Constants.PhieuXuatHuy.Select;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.PhieuXuatHuy phieuxuathuy = new Entities.PhieuXuatHuy();
                    phieuxuathuy.PhieuXuatHuyID = int.Parse(dr[Common.Constants.PhieuXuatHuy.PhieuXuatHuyID].ToString());
                    phieuxuathuy.MaPhieuXuatHuy = dr[Common.Constants.PhieuXuatHuy.MaPhieuXuatHuy].ToString();
                    phieuxuathuy.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuXuatHuy.NgayLap].ToString());
                    phieuxuathuy.MaNhanVien = dr[Common.Constants.PhieuXuatHuy.MaNhanVien].ToString();
                    phieuxuathuy.TenNhanVien = (dr[Common.Constants.PhieuXuatHuy.TenNhanVien].ToString());
                    phieuxuathuy.MaKho = dr[Common.Constants.PhieuXuatHuy.MaKho].ToString();
                    phieuxuathuy.TenKho = (dr[Common.Constants.PhieuXuatHuy.TenKho].ToString());
                    phieuxuathuy.TrangThai = bool.Parse(dr[Common.Constants.PhieuXuatHuy.TrangThai].ToString());
                    phieuxuathuy.Tongtien = (dr[Common.Constants.PhieuXuatHuy.Tongtien].ToString());
                    phieuxuathuy.GhiChu = dr[Common.Constants.PhieuXuatHuy.GhiChu].ToString();
                    phieuxuathuy.Deleted = bool.Parse(dr[Common.Constants.PhieuXuatHuy.Deleted].ToString());
                    arr.Add(phieuxuathuy);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.PhieuXuatHuy[0];
                }
                else
                {
                    list = new Entities.PhieuXuatHuy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.PhieuXuatHuy)arr[i];
                    }
                }
                
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.PhieuXuatHuy[0]; }
            return list;
        }

        public static Entities.PhieuXuatHuy[] Select1(Entities.PhieuXuatHuy nv)
        {
            Entities.PhieuXuatHuy[] list = null;
            try
            {
                string sql = Common.Constants.PhieuXuatHuy.Select1;
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.TrangThai, SqlDbType.Bit).Value = nv.TrangThai;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.PhieuXuatHuy phieuxuathuy = new Entities.PhieuXuatHuy();
                    phieuxuathuy.PhieuXuatHuyID = int.Parse(dr[Common.Constants.PhieuXuatHuy.PhieuXuatHuyID].ToString());
                    phieuxuathuy.MaPhieuXuatHuy = dr[Common.Constants.PhieuXuatHuy.MaPhieuXuatHuy].ToString();
                    phieuxuathuy.NgayLap = DateTime.Parse(dr[Common.Constants.PhieuXuatHuy.NgayLap].ToString());
                    phieuxuathuy.MaNhanVien = dr[Common.Constants.PhieuXuatHuy.MaNhanVien].ToString();
                    phieuxuathuy.TenNhanVien = (dr[Common.Constants.PhieuXuatHuy.TenNhanVien].ToString());
                    phieuxuathuy.MaKho = dr[Common.Constants.PhieuXuatHuy.MaKho].ToString();
                    phieuxuathuy.TenKho = (dr[Common.Constants.PhieuXuatHuy.TenKho].ToString());
                    phieuxuathuy.TrangThai = bool.Parse(dr[Common.Constants.PhieuXuatHuy.TrangThai].ToString());
                    phieuxuathuy.Tongtien = (dr[Common.Constants.PhieuXuatHuy.Tongtien].ToString());
                    phieuxuathuy.GhiChu = dr[Common.Constants.PhieuXuatHuy.GhiChu].ToString();
                    phieuxuathuy.Deleted = bool.Parse(dr[Common.Constants.PhieuXuatHuy.Deleted].ToString());
                    arr.Add(phieuxuathuy);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.PhieuXuatHuy[0];
                }
                else
                {
                    list = new Entities.PhieuXuatHuy[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.PhieuXuatHuy)arr[i];
                    }
                }

                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.PhieuXuatHuy[0]; }
            return list;
        }
        // insert update
        public static int InsertUpdate(Entities.PhieuXuatHuy phieuxuathuy)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.PhieuXuatHuy.InsertUpdate, cn);
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.HanhDong, SqlDbType.NVarChar, 50).Value = phieuxuathuy.HanhDong;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.PhieuXuatHuyID, SqlDbType.Int).Value = phieuxuathuy.PhieuXuatHuyID;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.NgayLap, SqlDbType.DateTime).Value = phieuxuathuy.NgayLap;

                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.MaPhieuXuatHuy, SqlDbType.VarChar, 50).Value = phieuxuathuy.MaPhieuXuatHuy;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.MaNhanVien, SqlDbType.VarChar, 50).Value = phieuxuathuy.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.TenNhanVien, SqlDbType.NVarChar, 200).Value = phieuxuathuy.TenNhanVien;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.MaKho, SqlDbType.VarChar, 50).Value = phieuxuathuy.MaKho;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.TenKho, SqlDbType.NVarChar, 200).Value = phieuxuathuy.TenKho;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.TrangThai, SqlDbType.Bit).Value = phieuxuathuy.TrangThai;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.Tongtien, SqlDbType.Float).Value = phieuxuathuy.Tongtien;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.GhiChu, SqlDbType.NVarChar, 200).Value = phieuxuathuy.GhiChu;
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.Deleted, SqlDbType.Bit).Value = phieuxuathuy.Deleted;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }
        // insert update
        public static int InsertUpdate(Entities.PhieuXuatHuy[] phieuxuathuy)
        {
            try
            {
                foreach (Entities.PhieuXuatHuy item in phieuxuathuy)
                {

                    if (InsertUpdate(item) == 0)
                    {
                        return 0;
                    }
                }
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        public static int Delete(Entities.PhieuXuatHuy phieuxuathuy)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.PhieuXuatHuy.Delete, cn);
                cmd.Parameters.Add(Common.Constants.PhieuXuatHuy.MaPhieuXuatHuy, SqlDbType.NVarChar, 50).Value = phieuxuathuy.MaPhieuXuatHuy;
                rt = cmd.ExecuteNonQuery();
                return rt;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }

        }

        public static void TruKho(Entities.ChiTietKhoHang[] DanhSach)
        {
            try
            {
                for (int k = 0; k < DanhSach.Length; k++)
                {
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand("exec sp_TruSoLuongTrongKho @MaKho,@MaHangHoa,@SoLuong", cn);
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaKho, SqlDbType.VarChar).Value = DanhSach[k].MaKho;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.MaHangHoa, SqlDbType.VarChar).Value = DanhSach[k].MaHangHoa;
                    cmd.Parameters.Add(Common.Constants.HDBanLe.SoLuong, SqlDbType.Float).Value = DanhSach[k].SoLuong;
                    int z = cmd.ExecuteNonQuery();
                }
            }
            catch { }
        }
    }
}
